<?php

// $Id: export_form.php, 29-08-2010 afterlastangel

require_once($CFG->libdir . '/formslib.php');

class mod_studentcp_subject_form extends moodleform {

    function definition() {
//        parent::definition();
        global $CFG, $USER;
        $mform = & $this->_form;
        $course = $this->_customdata['course'];
        $cm = $this->_customdata['cm'];
        $modcontext = $this->_customdata['modcontext'];
        $subjectid = $this->_customdata['subjectid'];
        $subjectname = $this->_customdata['subjectname'];
        if ($subjectid) {
            $subject = get_record('studentcp_subject', 'id ', $subjectid);
        }
        if ($subject) {

            $mform->addElement('hidden', 'id', $cm->id);

            $mform->addElement('hidden', 'subjectid', $subjectid);
            $mform->addElement('header', 'general', get_string('subjectmanagement', 'studentcp'));
            $mform->addElement('static', 'showsubjectid', get_string('subjectid', 'studentcp'), $subjectid);
            $mform->addElement('text', 'subjectcode', get_string('subjectcode', 'studentcp'), array('size' => '64', 'value' => $subject->subjectcode));
            $mform->setType('subjectcode', PARAM_TEXT);
            $mform->addRule('subjectcode', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'subjectname', get_string('subjectname', 'studentcp'), array('size' => '64', 'value' => $subject->name));
            $mform->setType('subjectname', PARAM_TEXT);
            $mform->addRule('subjectname', null, 'required', null, 'client');
            $mform->addRule('subjectname', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'category', get_string('category', 'studentcp'), array('size' => '64', 'value' => $subject->categoryid));
            $mform->setType('category', PARAM_TEXT);
            $mform->addRule('category', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'subjectcredit', get_string('credit', 'studentcp'), array('size' => '64', 'value' => $subject->credit));
            $mform->setType('subjectcredit', PARAM_INT);
            $mform->addRule('subjectcredit', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'description', get_string('description', 'studentcp'), array('size' => '64', 'value' => $subject->description));
            $mform->setType('description', PARAM_TEXT);
            $mform->addRule('description', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'gradedesc', get_string('gradedesc', 'studentcp'), array('size' => '64', 'value' => $subject->gradedesc, 'disabled' => 'disabled'));
            $mform->setType('gradedesc', PARAM_TEXT);
            $mform->addRule('gradedesc', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'gradetable', get_string('gradetable', 'studentcp'), array('size' => '64', 'value' => $subject->gradetable, 'disabled' => 'disabled'));
            $mform->setType('gradetable', PARAM_TEXT);
            $mform->addRule('gradetable', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');


            $maxuploadsize = $this->_customdata['maxuploadsize'];
            $this->set_upload_manager(new upload_manager('userfile', true, false, '', false, $maxuploadsize, true, true));

            $mform->addElement('checkbox', 'confirmdelete', get_string('deletethissubject','studentcp')
                    , get_string('deletethissubjecttooltips','studentcp'));
            $mform->addElement('file', 'userfile', '');
            $mform->setHelpButton('userfile', array('attachment', get_string('attachment', 'forum'), 'forum'));

            $mform->addElement('link', 'viewreport',  get_string('viewreport','studentcp'),
                    "./subjectmanagement.php?id=".$cm->id."&mode=view&subjectid=".$subjectid, get_string('viewreport','studentcp'),
                    '_target="blank"');


            $delete_string = get_string('delete','studentcp');
            $mform->addElement('submit', 'delete', $delete_string);

            $submit_string = get_string('update','studentcp');
            //$mform->
            $this->add_action_buttons(true, $submit_string);
        } else if ($subjectid == 0) {
            $mform->addElement('hidden', 'id', $cm->id);
            $mform->addElement('hidden', 'subjectid', $subjectid);
            $mform->addElement('header', 'general', get_string('subjectmanagement', 'studentcp'));
            $mform->addElement('static', 'showsubjectid', get_string('subjectid', 'studentcp'), $subjectid);
            $mform->addElement('text', 'subjectcode', get_string('subjectcode', 'studentcp'), array('size' => '64', 'value' => $subject->subjectcode));
            $mform->setType('subjectcode', PARAM_TEXT);
            $mform->addRule('subjectcode', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'subjectname', get_string('subjectname', 'studentcp'), array('size' => '64', 'value' => $subject->name));
            $mform->setType('subjectname', PARAM_TEXT);
            $mform->addRule('subjectname', null, 'required', null, 'client');
            $mform->addRule('subjectname', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'category', get_string('category', 'studentcp'), array('size' => '64', 'value' => $subject->categoryid));
            $mform->setType('category', PARAM_TEXT);
            $mform->addRule('category', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'subjectcredit', get_string('credit', 'studentcp'), array('size' => '64', 'value' => $subject->credit));
            $mform->setType('subjectcredit', PARAM_INT);
            $mform->addRule('subjectcredit', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'description', get_string('description', 'studentcp'), array('size' => '64', 'value' => $subject->description));
            $mform->setType('description', PARAM_TEXT);
            $mform->addRule('description', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'gradedesc', get_string('gradedesc', 'studentcp'), array('size' => '64', 'value' => $subject->gradedesc));
            $mform->setType('gradedesc', PARAM_TEXT);
            $mform->addRule('gradedesc', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $mform->addElement('text', 'gradetable', get_string('gradetable', 'studentcp'), array('size' => '64', 'value' => $subject->gradetable));
            $mform->setType('gradetable', PARAM_TEXT);
            $mform->addRule('gradetable', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

            $submit_string = get_string('insert','studentcp');
            //$mform->
            $this->add_action_buttons(true, $submit_string);
        }
    }

    function get_import_name() {
        if ($this->is_submitted() and $this->is_validated()) {
            // return the temporary filename to process
            return $this->_upload_manager->files['userfile']['tmp_name'];
        } else {
            return NULL;
        }
    }

}

?>
